FROM maven:3.8.4-openjdk-11 AS build
WORKDIR /app

# 首先只复制pom文件，利用Docker缓存机制
COPY pom.xml .
# 如果父项目有pom.xml，也需要复制
COPY ../pom.xml /parent-pom.xml
# 预先下载依赖项
RUN mvn dependency:go-offline -B || echo "依赖可能无法完全下载，但继续构建"

# 然后复制源代码
COPY src ./src
# 跳过测试并指定输出日志，以便排查问题
RUN mvn clean package -DskipTests -e

FROM openjdk:11-jre-slim
WORKDIR /app
COPY --from=build /app/target/*.jar app.jar
COPY src/main/resources/application.yml /app/application.yml

ENV SPRING_DATASOURCE_URL=jdbc:mysql://mysql:3306/photovoltaic?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=Asia/Shanghai&allowPublicKeyRetrieval=true
ENV SPRING_DATASOURCE_USERNAME=root
ENV SPRING_DATASOURCE_PASSWORD=changeit
ENV NEO4J_URI=bolt://neo4j:7687
ENV NEO4J_USERNAME=neo4j
ENV NEO4J_PASSWORD=password

EXPOSE 8082
ENTRYPOINT ["java", "-jar", "app.jar", "--spring.config.location=file:/app/application.yml"] 